package com.ruoyi.common.ehance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.query.QueryTool;

import java.util.List;

/**
 * @author xwj@kafukeji.cn
 * @date 2023/2/8 16:10
 */
public interface MyService<T extends BaseEntity> extends IService<T> {
    default T wrapperEntity(T entity) {
        return entity;
    }


    /**
     * 根据指定字段查询对应的值
     * @param column
     * @param value
     * @return
     */
    default T selectOne(String column, Object value) {
        List<T> list = this.list(column, value);
        return list.isEmpty() ? null : this.wrapperEntity(list.get(0));
    }

    /**
     * 根据指定字段查询对应的列表
     * @param column
     * @param value
     * @return
     */
    default List<T> list (String column, Object value) {
        QueryWrapper<T> queryWrapper = new QueryWrapper();
        queryWrapper.eq(column, value);
        return list(queryWrapper);
    }

    /**
     * 加载所有行，自动填充参数
     * @param entity
     * @return
     */
    default List<T> listAll(T entity) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        QueryTool.condition(qw, entity);
        return this.list(qw);
    }

    default boolean deleteBy(String column, Object value) {
        QueryWrapper<T> qw = new QueryWrapper();
        qw.eq(column, value);
        return remove(qw);
    }

}
